﻿package mortal.game.view.flowerSend.effect
{
    import __AS3__.vec.*;
    import baseEngine.modifiers.*;
    import com.greensock.*;
    import com.greensock.easing.*;
    import flash.events.*;
    import flash.utils.*;
    import frEngine.effectEditTool.temple.*;
    import mortal.game.scene3D.map3D.*;
    import mortal.game.scene3D.model.player.*;
    import mortal.game.scene3D.model.pools.*;

    public class FlowerEffect extends Object
    {
        private var _sendSmallWin:FlowerSendSmallWin;
        private var _stepVec:Vector.<Function>;
        private var _currentFunIndex:int = 0;
        private var _timeId:int;
        private var _sendTween:TweenMax;
        private var _recieveTween:TweenMax;
        public var isPlaying:Boolean;
        private var _linkEffectPlayer:EffectPlayer;
        private static var _instance:FlowerEffect;
        private static const LinkUrl:String = "UI_songhua_link01";

        public function FlowerEffect()
        {
            this._stepVec = new Vector.<Function>;
            return;
        }// end function

        private function createWin() : void
        {
            if (!this._sendSmallWin)
            {
                this._sendSmallWin = new FlowerSendSmallWin();
            }
            return;
        }// end function

        public function showEffect(param1:String, param2:String) : void
        {
            this.createWin();
            this.addEvent();
            this._sendSmallWin.show();
            this._sendSmallWin.sendSp.show((SceneRange.display.width >> 1) - 256, (SceneRange.display.height >> 1) - 128, true);
            this._sendSmallWin.recieveSp.show(SceneRange.display.width >> 1, (SceneRange.display.height >> 1) - 128, true);
            this._sendSmallWin.sendSp.playerName = param1;
            this._sendSmallWin.recieveSp.playerName = param2;
            this._stepVec.push(this.apart, this.shake, this.showAlpha, this.hideEffect);
            this.initialization();
            this.runFunction();
            this.isPlaying = true;
            return;
        }// end function

        private function addEvent() : void
        {
            this._sendSmallWin.addEventListener(Event.COMPLETE, this.checkAddLinkEffect);
            return;
        }// end function

        private function hideLinkEffect() : void
        {
            this._sendSmallWin.removeEventListener(Event.COMPLETE, this.checkAddLinkEffect);
            if (this._linkEffectPlayer)
            {
                this._linkEffectPlayer.dispose();
                this._linkEffectPlayer = null;
            }
            return;
        }// end function

        private function checkAddLinkEffect(event:Event) : void
        {
            if (this._sendSmallWin.sendSp.loaded && this._sendSmallWin.recieveSp.loaded)
            {
                this.addLinkEffect();
            }
            return;
        }// end function

        private function addLinkEffect() : void
        {
            if (!this._linkEffectPlayer)
            {
                this._linkEffectPlayer = EffectPlayerPool.instance.getEffectPlayer(LinkUrl, null, this._sendSmallWin.rect3d.renderList, false);
                if (this._linkEffectPlayer.temple is TempleFight)
                {
                    (this._linkEffectPlayer.temple as TempleFight).setFightParams(this._sendSmallWin.sendSp.effectPlayer, this._sendSmallWin.recieveSp.effectPlayer, null, false, null, -1);
                    this._linkEffectPlayer.play(PlayMode.ANIMATION_LOOP_MODE);
                    this._linkEffectPlayer.impRemove = true;
                    this._linkEffectPlayer.parent = null;
                }
                this._sendSmallWin.rect3d.addObject3d(this._linkEffectPlayer, 0, 0);
            }
            return;
        }// end function

        private function initialization() : void
        {
            this._currentFunIndex = 0;
            clearTimeout(this._timeId);
            this._sendSmallWin.alpha = 1;
            this.killTween();
            return;
        }// end function

        private function runFunction(... args) : void
        {
            this._stepVec[this._currentFunIndex].call(null, args);
            args = this;
            var _loc_3:* = args._currentFunIndex + 1;
            args._currentFunIndex = _loc_3;
            return;
        }// end function

        private function setDurationTime(param1:Number) : void
        {
            this._timeId = setTimeout(this.runFunction, param1 * 1000);
            return;
        }// end function

        private function killTween() : void
        {
            if (this._sendTween && this._sendTween.active)
            {
                this._sendTween.kill();
            }
            if (this._recieveTween && this._recieveTween.active)
            {
                this._recieveTween.kill();
            }
            return;
        }// end function

        private function showWin(param1:Object) : void
        {
            this._sendTween = TweenMax.to(this._sendSmallWin.sendSp, 0.9, {y:this._sendSmallWin.sendSp.y - 500, alpha:1, ease:Quint.easeOut});
            this._recieveTween = TweenMax.to(this._sendSmallWin.recieveSp, 0.9, {y:this._sendSmallWin.recieveSp.y - 500, alpha:1, ease:Quint.easeOut});
            this.setDurationTime(0.9);
            return;
        }// end function

        private function apart(param1:Object) : void
        {
            this._sendTween = TweenMax.to(this._sendSmallWin.sendSp, 0.6, {x:(SceneRange.display.width >> 1) - 256 - 100, ease:Quint.easeOut});
            this._recieveTween = TweenMax.to(this._sendSmallWin.recieveSp, 0.6, {x:(SceneRange.display.width >> 1) + 100, ease:Quint.easeOut});
            this.setDurationTime(0.7);
            return;
        }// end function

        private function shake(param1:Object) : void
        {
            this.setDurationTime(2);
            return;
        }// end function

        private function showAlpha(param1:Object) : void
        {
            this._sendTween = TweenMax.to(this._sendSmallWin.sendSp, 1.2, {alpha:0, ease:Quint.easeOut});
            this._recieveTween = TweenMax.to(this._sendSmallWin.recieveSp, 1.2, {alpha:0, ease:Quint.easeOut});
            this.setDurationTime(1.2);
            return;
        }// end function

        private function hideEffect(param1:Object) : void
        {
            this._sendSmallWin.hide();
            this.hideLinkEffect();
            this.killTween();
            this.isPlaying = false;
            return;
        }// end function

        public static function get instance() : FlowerEffect
        {
            if (_instance == null)
            {
                _instance = new FlowerEffect;
            }
            return _instance;
        }// end function

    }
}
